package ${package}.controller;
import ${package}.pojo.${entity};
<#if childTable??>
import ${package}.pojo.VO.${entity}VO;
</#if>
import com.hifar.plat.baseModal.pojo.BaseOssFiles;
import com.hifar.plat.baseModal.service.IBaseOssFilesService;
import ${package}.service.I${entity}Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import ${package}.service.I${entity}Service;
import io.swagger.annotations.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import com.hifar.sys.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.hifar.proj.material.pojo.Material;
import com.hifar.fw.utils.bean.BusiConst;
import com.hifar.proj.material.service.IMaterialService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * ${table.comment} 前端控制器 default
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
<#if prefix??>
@RequestMapping("/api/busi/${prefix}/${entity?uncap_first}Controller")
<#else>
@RequestMapping("/api/busi/${entity?uncap_first}Controller")
</#if>
@Api(tags = "${table.comment}功能")
public class ${entity}Controller {

    @Autowired
    private I${entity}Service ${entity?uncap_first}Service;

    @Autowired
    private IBaseOssFilesService baseOssFilesService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "${table.comment}功能:分页查询列表")
    public BusiResponse list(HttpServletRequest request) {
        Map<String,Object> pageInfo = RequestConver.getPageInfo(request);
        QueryWrapper<${entity}> queryWrapper = RequestConver.getPostParam(pageInfo);
        queryWrapper.ne("del_flag", "1");
        queryWrapper.orderByDesc("create_time");
        queryWrapper.in("secret_level",SystemLoginUser.dataSecretSet);
        if (queryWrapper != null && pageInfo.size() > 0) {
             Page<${entity}> page = new Page<>((Integer) pageInfo.get("pageNo"), (Integer)pageInfo.get("pageSize"));
             Page<${entity}> page1 = ${entity?uncap_first}Service.page(page, queryWrapper);
             page1.getRecords().forEach(item->{
                <#list table.fields as field>
                <#if field.fieldShowType != "" && field.fieldShowType == "9">
                if (item.get${field.propertyName?cap_first}() != null && item.get${field.propertyName?cap_first}().toString().length() > 0){
                    item.set${field.propertyName?cap_first}(baseOssFilesService.list(new QueryWrapper<BaseOssFiles>().in("id", item.get${field.propertyName?cap_first}().toString().split(","))));
                }
                </#if>
                </#list>
             });
             /*计算分页信息返回给前端*/
             PageResult pageResult = SystemUtil.parsePage(page, page1);
             return BusiResponse.OK(pageResult);
        }else {
             return BusiResponse.error("参数不正确！");
        }
    }

    @PostMapping(value = "/listAll")
    @ApiOperation(value = "${table.comment}功能:查询列表(不带分页)")
    public BusiResponse listAll(HttpServletRequest request) {
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.ne("del_flag", "1");
        queryWrapper.in("secret_level",SystemLoginUser.dataSecretSet);
        List<${entity}> list = ${entity?uncap_first}Service.list(queryWrapper);
        list.forEach(item->{
            <#list table.fields as field>
            <#if field.fieldShowType != "" && field.fieldShowType == "9">
            if (item.get${field.propertyName?cap_first}() != null && item.get${field.propertyName?cap_first}().toString().length() > 0){
                item.set${field.propertyName?cap_first}(baseOssFilesService.list(new QueryWrapper<BaseOssFiles>().in("id", item.get${field.propertyName?cap_first}().toString().split(","))));
            }
            </#if>
            </#list>
        });
        if (list != null && list.size() > 0) {
            return BusiResponse.OK(list);
        }else {
            return BusiResponse.ok("没有查询到数据！");
        }
    }

<#if childTable??>
    @PostMapping(value = "/add")
    @ApiOperation(value = "${table.comment}功能:新增")
    public BusiResponse add(@RequestBody ${entity}VO ${entity?uncap_first}VO) {
        ${entity?uncap_first}Service.save${entity}(${entity?uncap_first}VO);
        return BusiResponse.ok("添加成功");
    }




    @PostMapping(value = "/edit")
    @ApiOperation(value = "${table.comment}功能:编辑")
    public BusiResponse edit(@RequestBody ${entity}VO ${entity?uncap_first}VO) {
        ${entity?uncap_first}Service.edit${entity}(${entity?uncap_first}VO);
        return BusiResponse.ok("编辑成功！");
    }




    @GetMapping("/queryVOById")
    @ApiOperation(value = "${table.comment}功能:根据id查询数据")
    public BusiResponse queryVOById(@RequestParam(value = "id") String id){
        ${entity}VO byId = ${entity?uncap_first}Service.query${entity}VO(id);
        <#list table.fields as field>
        <#if field.fieldShowType != "" && field.fieldShowType == "9">
        if (byId.get${field.propertyName?cap_first}() != null && byId.get${field.propertyName?cap_first}().toString().length() > 0){
            byId.set${field.propertyName?cap_first}(baseOssFilesService.list(new QueryWrapper<BaseOssFiles>().in("id", byId.get${field.propertyName?cap_first}().toString().split(","))));
        }
        </#if>
        </#list>
        return BusiResponse.OK(byId);
    }


    @PostMapping(value = "/save")
    @ApiOperation(value = "${table.comment}功能:新增")
    public BusiResponse save(@RequestBody ${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}Service.save(${entity?uncap_first});
        return BusiResponse.ok("添加成功");
    }





    @PostMapping(value = "/modify")
    @ApiOperation(value = "${table.comment}功能:编辑")
    public BusiResponse modify(@RequestBody ${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}Service.updateById(${entity?uncap_first});
        return BusiResponse.ok("编辑成功！");
    }




    @GetMapping("/queryById")
    @ApiOperation(value = "${table.comment}功能:根据id查询数据")
    public BusiResponse queryById(@RequestParam(name = "id") String id){
        ${entity} byId = ${entity?uncap_first}Service.getById(id);
        // 查询转换附件
        <#list table.fields as field>
        <#if field.fieldShowType != "" && field.fieldShowType == "9">
        if (byId.get${field.propertyName?cap_first}() != null && byId.get${field.propertyName?cap_first}().toString().length() > 0){
            byId.set${field.propertyName?cap_first}(baseOssFilesService.list(new QueryWrapper<BaseOssFiles>().in("id", byId.get${field.propertyName?cap_first}().toString().split(","))));
        }
        </#if>
        </#list>
        return BusiResponse.OK(byId);
    }

<#else >
    @PostMapping(value = "/add")
    @ApiOperation(value = "${table.comment}功能:新增")
    public BusiResponse add(@RequestBody ${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}Service.save(${entity?uncap_first});
        return BusiResponse.ok("添加成功");
    }





    @PostMapping(value = "/edit")
    @ApiOperation(value = "${table.comment}功能:编辑")
    public BusiResponse edit(@RequestBody ${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}Service.updateById(${entity?uncap_first});
        return BusiResponse.ok("编辑成功！");
    }




    @GetMapping("/queryById")
    @ApiOperation(value = "${table.comment}功能:根据id查询数据")
    public BusiResponse queryById(@RequestParam(name = "id") String id){
        ${entity} byId = ${entity?uncap_first}Service.getById(id);
        // 查询转换附件
        <#list table.fields as field>
        <#if field.fieldShowType != "" && field.fieldShowType == "9">
        if (byId.get${field.propertyName?cap_first}() != null && byId.get${field.propertyName?cap_first}().toString().length() > 0){
            byId.set${field.propertyName?cap_first}(baseOssFilesService.list(new QueryWrapper<BaseOssFiles>().in("id", byId.get${field.propertyName?cap_first}().toString().split(","))));
        }
        </#if>
        </#list>
        return BusiResponse.OK(byId);
    }
</#if>




    @PostMapping("/delete")
    @ApiOperation(value = "${table.comment}功能:逻辑删除")
    public BusiResponse delete(@RequestBody List<String> idList){
        QueryWrapper<${entity}> delete = new QueryWrapper<>();
        delete.in("id",idList);
        ${entity?uncap_first}Service.remove(delete);
        return BusiResponse.ok("删除成功！");
    }
   
  <#if table.tableType == '2' >
    @PostMapping("/exportExcel")
	@ApiOperation(value = "${table.comment}功能:导出数据")
	public Object exportExcel(HttpServletRequest request) {
		Map<String, Object> pageInfo = RequestConver.getPageInfo(request);
		if (pageInfo == null) {
			return BusiResponse.error("参数有误！");
		}
		BusiResponse export = ${entity?uncap_first}Service.exportExcel(pageInfo);
		if (export.getCode() == com.hifar.fw.utils.bean.BusiConst.CODE_SUCCESS) {
			String encode = null;
			try {
				encode = URLEncoder.encode("${table.comment}.xlsx", "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			HttpHeaders headers = new HttpHeaders();
			headers.add("Content-Type", "application/vnd.ms-excel");
			headers.add("Access-Control-Expose-Headers", "Content-Disposition");
			headers.add("Content-Disposition", "filename=" + encode);
			return ResponseEntity.status(HttpStatus.OK).headers(headers).body((byte[]) export.getData());
		}
		return export;
	}

	@PostMapping("/importExcel")
	@ApiOperation(value = "${table.comment}功能:导入excel数据")
	public BusiResponse importExcel(@RequestParam("file") MultipartFile file,@RequestParam("mainId") String  mainId) throws Exception {
		if (file.isEmpty()) {
			return BusiResponse.error("文件为空！");
		} else {
			if (ExcelTemplateUtil.isExcelFile(file)) {
				return ${entity?uncap_first}Service.importExcel(file,mainId);
			} else {
				return BusiResponse.error("请上传excel文件！");
			}
		}
	}
  <#else>
    @PostMapping("/exportExcel")
	@ApiOperation(value = "${table.comment}功能:导出数据")
	public Object exportExcel(HttpServletRequest request) {
		Map<String, Object> pageInfo = RequestConver.getPageInfo(request);
		if (pageInfo == null) {
			return BusiResponse.error("参数有误！");
		}
		BusiResponse export = ${entity?uncap_first}Service.exportExcel(pageInfo);
		if (export.getCode() == com.hifar.fw.utils.bean.BusiConst.CODE_SUCCESS) {
			String encode = null;
			try {
				encode = URLEncoder.encode("${table.comment}.xlsx", "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			HttpHeaders headers = new HttpHeaders();
			headers.add("Content-Type", "application/vnd.ms-excel");
			headers.add("Access-Control-Expose-Headers", "Content-Disposition");
			headers.add("Content-Disposition", "filename=" + encode);
			return ResponseEntity.status(HttpStatus.OK).headers(headers).body((byte[]) export.getData());
		}
		return export;
	}

	@PostMapping("/importExcel")
	@ApiOperation(value = "${table.comment}功能:导入excel数据")
	public BusiResponse importExcel(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			return BusiResponse.error("文件为空！");
		} else {
			if (ExcelTemplateUtil.isExcelFile(file)) {
				return ${entity?uncap_first}Service.importExcel(file);
			} else {
				return BusiResponse.error("请上传excel文件！");
			}
		}
	}
  </#if> 
    

}